package com.hula.onion.book.server.spider.platforms.yilongdai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.spider.platforms.xinyongbao.XinYongBaoSpiderHelper;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Description:翼龙贷
 * Create Date:2018/1/17 19:33
 *
 * @author liangbing
 * @version 1.0
 */
@Component("yiLongDaiSpider")
public class YiLongDaiSpider extends SpiderCommon {

    @Autowired
    private YiLongDaiSpiderHelper yiLongDaiSpiderHelper;

    private static final String LOGIN_URL = "https://passport.eloancn.com/login";

    private static final String ACCOUNT_INFO_URL = "https://licai.eloancn.com/account/index.html";

    @Override
    public CommonResponse execute() {

        HttpClientContext httpContext = new HttpClientContext();
        BasicCookieStore cookieStore = new BasicCookieStore();
        httpContext.setCookieStore(cookieStore);

        Map<String, Object> params = yiLongDaiSpiderHelper.installRequestParams(loginForm);

        String loginStr = HttpUtils.post(LOGIN_URL, params, httpContext);

        if (StringUtils.isEmpty(loginStr)) {
            return new CommonResponse(ResponseStatusEnum.FAIL);
        }
        JSONObject loginJson = JSON.parseObject(loginStr);

        if (loginJson.getBoolean("success")) {

            String accountStr = HttpUtils.get(ACCOUNT_INFO_URL + System.currentTimeMillis(), httpContext);
           // String account = spiderHelper.parseAccountBalance(accountStr);
            CommonResponse commonResponse = new CommonResponse(ResponseStatusEnum.SUCCESS);
            //commonResponse.setData(account);
            return commonResponse;
        } else {
            return new CommonResponse(ResponseStatusEnum.ACCOUNT_ERROR);
        }
    }
}
